


//
//  StatusViewModel.swift
//  Weibo
//
//  Created by gdarkness on 16/3/27.
//  Copyright © 2016年 gdarkness. All rights reserved.
//

import UIKit

class StatusViewModel: NSObject {
    //单例
    static let shareInstance: StatusViewModel = StatusViewModel()
    
    
    
    func loadStatus(loadStatusCallBack: (statuses: [Status]?, error: NSError?) -> ()) {
        //url
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        //请求参数
        let parameters = ["access_token" : UserAccountViewModel.shareInstance.userAccount!.access_token!]
        
        NetWorkTool.shareInstance.requestGET(urlString, parameters: parameters) { (requst, error) -> () in
            if error != nil{
                //出现错误
                print("请求出错了 \(error)")
                return
            }
            //没有出错，解析返回的数据中，"statuses",statuses里面对应的是所有微博的数据
            let statusesArray = requst?["statuses"] as? [[String: AnyObject]]
            
            //定义数组来存放微博数据
            var statuses = [Status]()
            
            //遍历微博字典数组
            for dict in statusesArray! {
                //字典转模型
                let status = Status(dict: dict)
                statuses.append(status)
            }
            //字典转模型完成
            loadStatusCallBack(statuses: statuses, error: nil)
        }
    }
}
